package api

import (
	"context"

	"dev.ixmxm.top/rms/codes"
	"dev.ixmxm.top/rms/utils/log"
)

// @Summary 用户数据添加
// @Description
// @Accept application/json
// @Produce application/json
// @Param body body SaveUserParams true "新增的用户数据"
// @Success 0 {object} ResponseExample "返回数据格式"
// @Tags 用户管理
// @Router /sys_user/add_sys_user [post]
func (hg *HttpGateway) AddSysUser(ctx context.Context, params interface{}) (interface{}, codes.Code) {
	p := params.(*SaveUserParams)
	//验证数据是否正常
	err := hg.dispatcher.UM().AddUserToDB(ctx, &p.User)
	if err != nil {
		log.ErrorfWithContext(ctx, "add one user error:%s", err.UnWrapError())
		return nil, err
	}
	return nil, nil
}

// @Summary 用户数据修改
// @Description
// @Accept application/json
// @Produce application/json
// @Param body body SaveUserParams true "更新的用户数据"
// @Success 0 {object} ResponseExample "返回数据格式"
// @Tags 用户管理
// @Router /sys_user/update_sys_user [post]
func (hg *HttpGateway) UpdateSysUser(ctx context.Context, params interface{}) (interface{}, codes.Code) {
	p := params.(*SaveUserParams)
	//验证数据是否正常
	err := hg.dispatcher.UM().UpdateUserToDB(ctx, &p.User)
	if err != nil {
		log.ErrorfWithContext(ctx, "update one user error:%s", err.UnWrapError())
		return nil, err
	}
	return nil, nil
}

// @Summary 删除用户根据帐号
// @Description
// @Accept application/json
// @Produce application/json
// @Param account query string true "用户帐号"
// @Success 0 {object} ResponseExample "返回数据格式"
// @Tags 用户管理
// @Router /sys_user/delete_sys_user [delete]
func (hg *HttpGateway) DeleteSysUserByAccount(ctx context.Context, params interface{}) (interface{}, codes.Code) {
	p := params.(*UserAccountParams)
	err := hg.dispatcher.UM().DeleteUserToDB(ctx, p.Account)
	if err != nil {
		log.ErrorfWithContext(ctx, "delete one user by account error:%s", err.UnWrapError())
		return nil, err
	}
	return nil, nil
}

// @Summary 获取所有用户
// @Description
// @Accept application/json
// @Produce application/json
// @Success 1 {object} []model.User "data数据:用户集合"
// @Tags 用户管理
// @Router /sys_user/get_all_sys_user [get]
func (hg *HttpGateway) GetAllSysUsers(ctx context.Context, params interface{}) (interface{}, codes.Code) {
	res, err := hg.dispatcher.UM().GetAllUserFromDB(ctx)
	if err != nil {
		log.ErrorfWithContext(ctx, "get all users error:%s", err.UnWrapError())
		return nil, err
	}
	return res, nil
}

// @Summary 验证用户登录
// @Description
// @Accept application/json
// @Produce application/json
// @Param body body AccountVerifyParams true "用户验证"
// @Success 0 {object} ResponseExample "返回数据格式"
// @Tags 用户管理
// @Router /sys_user/account_verify [post]
func (hg *HttpGateway) VerifyUser(ctx context.Context, params interface{}) (interface{}, codes.Code) {
	p := params.(*AccountVerifyParams)
	//获取充电桩信息
	user, err := hg.dispatcher.UM().GetOneUserFromDB(ctx, p.Account)
	if err != nil {
		log.ErrorfWithContext(ctx, "get user by account  error:%s", err.UnWrapError())
		return nil, err
	}
	if user == nil {
		log.ErrorfWithContext(ctx, "account  error!!!")
		return nil, codes.ErrNotExitUserAccount
	}
	//验证密码是否一致
	if user.Passwd != p.Passwd {
		log.ErrorfWithContext(ctx, "passwd error!!!")
		return nil, codes.ErrUserPassWd
	}
	return user, nil
}
